﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using System.Threading;
using System.Runtime.Remoting.Messaging;
using SAServices.RemtInvtor;

namespace InventorRunner
{
    public partial class Form1 : Form
    {

        bool isStop = false;
        TcpChannel tcpChannel = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            doAllRunDelegat dlgDoAllRun = new doAllRunDelegat(doAllRun);
            //invetor起動処理
            dlgDoAllRun.BeginInvoke(new AsyncCallback(CallBackMethod), null);

            label1.Text = "Inventor起動中。。。";
        }


        delegate bool doAllRunDelegat();
        private bool doAllRun()
        {
            tcpChannel = new TcpChannel(90);
            ChannelServices.RegisterChannel(tcpChannel, false);
            Type commonInterfaceType = typeof(InvtRmtService);

            RemotingConfiguration.RegisterWellKnownServiceType(commonInterfaceType,
                "InventorStart", WellKnownObjectMode.SingleCall);

            InvtRmtService.GetInventorApp();

            while (isStop)
            {
                Thread.Sleep(10000);
            }
            return true;
        }
        public void CallBackMethod(IAsyncResult result)
        {
            doAllRunDelegat deleg = ((AsyncResult)result).AsyncDelegate as doAllRunDelegat;
            //bool isResult = deleg.EndInvoke(result);
            //// ボタンの使用かどうか設定
            //if (isResult == false)
            //{
            //    //eventLog1.WriteEntry("Remoteサービス失敗しました。");
            //}
            //else
            //{
            //    //eventLog1.WriteEntry("Remoteサービス成功しました。");
            //    //runSuccessed(frmSprush, clsForm.cmbSyoriKbn.Text);
            //}

        }

        private void button2_Click(object sender, EventArgs e)
        {
            isStop = false;
            label1.Text = "Inventor停止しました。";
            if(tcpChannel!=null)
            {
                ChannelServices.UnregisterChannel(tcpChannel);
            }
           
        }

    }
}
